package com.ita.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.dao.UserDao;
import com.ita.dao.impl.UserDaoImpl;
import com.ita.pojo.User;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
//		boolean isError=false;
//		String errorMsg="";
		String username=request.getParameter("username");
		String password=request.getParameter("password");
//		if(username.length()>4){
//			errorMsg="username should be less than 4 length";
//			isError=true;
//		}
//		if(password.length()<5){
//			errorMsg="username should be greater than 8 length";
//			isError=true;
//		}
		
		
		UserDao userDao=new UserDaoImpl();
		User user=userDao.findUser(username, password);
		if(user==null){
			request.setAttribute("msg", "登录失败");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}else {
			HttpSession session=request.getSession();
			session.setAttribute("user", user);
			request.getRequestDispatcher("booklist").forward(request,
					response);
		}
		
				
	}

}
